<?php
/**
 * Semester Score Controller
 * Semester Score Information   Management
 *
 * semester entry
 *
 * @version $Id: ScoreController.php 93 2010-05-02 03:33:24Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package semester
 * @subpackage Controller
 */
class semester_ScoreController extends Zend_Controller_Action
{


    function indexAction()
    {
        $this->_redirect('semester/score/set');

    }

    function setAction()
    {
        //學年學期
        if (!$this->_request->getParam('year_semester'))
        $this->view->year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
        else
        $this->view->year_semester = $this->_request->getParam('year_semester');
        //年級
        if (!$this->view->sel_grade = $this->_request->getParam('sel_grade'))
        // 預設最低學年
        $this->view->sel_grade = $this->view->lowYear;

    }

    /**
     * 存入成績設定
     *
     */
    function editAction()
    {
        $arr = explode("-",$this->_request->getParam('year_semester'));
        $year = (int)$arr[0];
        $semester = (int)$arr[1];
        $grade = (int)$this->_request->getParam('sel_grade');
        $period = (int)$this->_request->getParam('period');
        $mode = $this->_request->getParam('mode');
        if ($mode==1) { //相同比例
            $mode_1 = (float)$this->_request->getParam('mode-1');
            $mode_2 = (float)$this->_request->getParam('mode-2');
            $ratio = "$mode_1-$mode_2";
        }else { // 不同比例
            $arr = array();
            for($i=1;$i<=$period;$i++) {
                $arr[] = (float)$this->_request->getParam("m-1-$i").'-'.(float)$this->_request->getParam("m-2-$i");
            }
            $ratio = implode(",",$arr);
        }
        // 等第數
        $jset_num = (int)$this->_request->getParam('jset_num');
        $arr = array();
        $js = $this->_request->getParam('js');
        $ps = $this->_request->getParam('ps');
        $ns = $this->_request->getParam('ns');
        for($i=1;$i<=$jset_num;$i++) {
            $arr[] = $js[$i].'_'.$ps[$i].'_'.(float)$ns[$i];
        }
        $judgement = implode("\n",$arr);

        //        $semester_examin = new Semester_Model_DbTable_SemesterExamine();

        $s = $grade;
        $e = $grade;
        if ($this->_request->getParam('all_same')) { // 所有年級相同設定
            $s = $this->view->lowYear;
            $e = $this->view->highYear;
        }
        for($i=$s; $i<= $e ;$i++) {
            $semesterExamin = new Semester_Model_SemesterExamine();


            $q = Doctrine_Query::create()
            ->from('Semester_Model_SemesterExamine')
            ->where('year=?', $year)
            ->andWhere('semester=?', $semester)
            ->andWhere('grade=?',$i);
            if ($row = $q->fetchOne()) {
                $semesterExamin->assignIdentifier($row->sn);
            }
            else {
                $semesterExamin->year = $year;
                $semesterExamin->semester = $semester;
                $semesterExamin->grade = $i;
            }
            $semesterExamin->ratio = $ratio;
            $semesterExamin->mode = $mode;
            $semesterExamin->judgement = $judgement;
            $semesterExamin->period = $period;
            $semesterExamin->enable = 'Y';
            $semesterExamin->update_time = date("Y-m-d H:i:s");
            $semesterExamin->save();
        }
        $this->_helper->json('');

    }

    /**
     * 取出年級成績設定 以 json 資料輸出
     *
     */
    function getsetAction()
    {
        $arr = explode("-",$this->_request->getParam('year_semester'));
        $year = (int)$arr[0];
        $semester = (int)$arr[1];
        $cyear = (int)$this->_request->getParam('cyear');
        $q = Doctrine_Query::create()
        ->from('Semester_Model_SemesterExamine')
        ->where('year=?',$year)
        ->andwhere('semester=?',$semester)
        ->andWhere('grade=?',$cyear);

        $res = $q->fetchOne();

        $arr = array();
        // 等第預設值
        if (!$res){
            $semesterExamine = new Semester_Model_SemesterExamine();
            $arr['judge'] = $semesterExamine->getDefaultJudge();
            $arr['period'] = 2; // 預設考查次數
            $arr['period_mode'] = 1; // 預設相同比例
            $arr['period_1']   = array(1=>50,2=>50);
            $arr['period_2'] = array();
            $arr['judge_num'] = 5;
        }
        else {
            $arr['period'] = $res->period; // 考查次數
            $arr['period_mode'] = $res->mode; // 比例方式
            if ($res->mode == 1) {
                $temp = explode("-",$res->ratio);
                $arr['period_1'] = array(1=>$temp[0],2=>$temp[1]);
                $arr['period_2'] = array();
            }else {
                $temp = explode(",",$res->ratio);
                foreach($temp as $id=>$val)
                $arr['period_2'][$id+1] = explode("-",$val);
                $arr['period_1'] = array();
            }
            $temp = explode("\n",$res->judgement);
            foreach($temp as $id=>$val) {
                $arr['judge'][$id+1] = explode("_",$val);
            }
            $arr['judge_num'] = count($temp);
        }

        $this->_helper->json($arr);
    }
}
